package 笔试;

import java.util.Arrays;
import java.util.HashMap;

public class 判断是否是顺子 {
    public static boolean is_straight (String[] cards) {
        // write code here
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("A","1");
        map.put("J","11");
        map.put("Q","12");
        map.put("K","13");

        int isWang = 0;
        int j = 0;
        for(int i = 0; i < cards.length; i++) {
            if("大王".equals(cards[i]) || "小王".equals(cards[i])) {
                isWang++;
            }
        }
        int[] tmp = new int[5-isWang];
        for(int i = 0; i < cards.length; i++) {
            if("大王".equals(cards[i]) || "小王".equals(cards[i])) {
                continue;
            }else if(map.containsKey(cards[i])) {
                tmp[j++] = Integer.parseInt(map.get(cards[i]));
            } else {
                tmp[j++] = Integer.parseInt(cards[i]);
            }
        }
        Arrays.sort(tmp);
        for(int i = 0; i < tmp.length; i++) {
            if(i+1 < tmp.length && tmp[i]+1 == tmp[i+1]) {
                continue;
            } else if(i+1 < tmp.length) {
                int a = tmp[i];
                while(tmp[i+1] - a > 1) {
                    if(isWang > 0) {
                        isWang--;
                        a++;
                    } else {
                        return false;
                    }
                }
            } else {
                break;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String[] a = {"A","3","2","大王","4"};

        System.out.println(is_straight(a));
    }
}
